package com.feng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.entity.RestResponse;
import com.feng.entity.vo.ChatRoomPageVo;
import com.feng.mapper.ChatRoomMapper;
import com.feng.entity.ChatRoom;
import com.feng.service.ChatRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * (ChatRoom)表服务实现类
 *
 * @author feng
 * @since 2025-06-09 21:56:50
 */
@Service
public class ChatRoomServiceImpl extends ServiceImpl<ChatRoomMapper, ChatRoom> implements ChatRoomService {

    @Autowired
    private ChatRoomMapper chatRoomMapper;

    @Transactional
    @Override
    public boolean save(ChatRoom entity) {
        chatRoomMapper.AUTO_INCREMENT();
        return chatRoomMapper.insert(entity) > 0 ? true : false;
    }


    @Override
    public RestResponse getChatRoom(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<ChatRoom> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(ChatRoom::getTime);
        Page<ChatRoom> page = new Page<>(pageNum, pageSize);
        List<ChatRoom> chatRooms = list(page, wrapper);
        Collections.reverse(chatRooms);
        return RestResponse.success(new ChatRoomPageVo(chatRooms, page.getTotal()));
    }
}

